using UnityEngine;
using System.Collections;


public class DeathTrigger : MonoBehaviour 
{
	public bool soundTrigger = false;
	public bool isStair = false;
	public bool isLastStair = false;
	public int stairSize = 3;
	private GameObject player1;
	private GameObject player2;
	// Use this for initialization
	void Start () 
	{
		player1 = GameObject.FindGameObjectWithTag("Player1");
		player2 = GameObject.FindGameObjectWithTag("Player2");
	}
	
	// Update is called once per frame
	void Update () 
	{
		if((player1.transform.position.x>=transform.position.x-(transform.lossyScale.x/2) || player2.transform.position.x>=transform.position.x-(transform.lossyScale.x/2)) && !soundTrigger)
		{
			soundTrigger = true;
			
			if(isStair)
			{
				if(GameObject.FindGameObjectWithTag("Notes").GetComponent<Notes>()!=null)
				{
					GameObject.FindGameObjectWithTag("Notes").GetComponent<Notes>().playStairs(stairSize, isLastStair);
				}
			}
			else
			{
				if(GameObject.FindGameObjectWithTag("Notes").GetComponent<Notes>()!=null)
				{
					GameObject.FindGameObjectWithTag("Notes").GetComponent<Notes>().playNote();
				}
			}
		}
	}
	 
	void OnTriggerEnter(Collider cObject)
	{		
		if(cObject.tag == "Player1" || cObject.tag == "Player2")
		{
			if(cObject.tag == "Player1")
			{
				foreach(GameObject go in GameObject.FindGameObjectsWithTag("Pickup1"))
				{
					Destroy(go);
				}
			}
			else if(cObject.tag == "Player2")
			{
				foreach(GameObject go in GameObject.FindGameObjectsWithTag("Pickup2"))
				{
					Destroy(go);
				}
			}
			
			playerDeath(cObject);
		}
	}
	
	void playerDeath(Collider cObject)
	{
		cObject.gameObject.transform.position = new Vector3 (0, 0, cObject.gameObject.transform.position.z);
		cObject.rigidbody.velocity = Vector3.zero;
		cObject.gameObject.GetComponent<Movement>().isAlive = false;
		
		GameObject camera = GameObject.FindGameObjectWithTag("MainCamera");
		camera.GetComponent<AudioSource>().Play();
		
		if (camera.GetComponent<SmoothFollow>().target.tag=="Player1" && cObject.tag == "Player1" )
		{
			var temp = camera.GetComponent<SmoothFollow>().target;
			camera.GetComponent<SmoothFollow>().target = camera.GetComponent<SmoothFollow>().target2;
			camera.GetComponent<SmoothFollow>().target2 = temp;
		}
		else if (camera.GetComponent<SmoothFollow>().target.tag=="Player2" && cObject.tag == "Player2")
		{
			var temp = camera.GetComponent<SmoothFollow>().target;
			camera.GetComponent<SmoothFollow>().target = camera.GetComponent<SmoothFollow>().target2;
			camera.GetComponent<SmoothFollow>().target2 = temp;
		}
		 
		PlayerData.scoreMultiplier*=3;
		
//		GameObject[] gameObj = GameObject.FindGameObjectsWithTag("Obstacle");
//		
//		for(int i =0; i<gameObj.Length; i++)
//		{
//			if(gameObj[i].GetComponentInChildren<DeathTrigger>()!=null)
//			{
//				gameObj[i].GetComponentInChildren<DeathTrigger>().soundTrigger = false;
//			}
//		}
	}
	
	void printNewValues()
	{
		Debug.Log("JumpForce = "+PlayerData.jumpForce);
		Debug.Log("hjforce: "+PlayerData.holdJumpForce);
		Debug.Log("nhjForce: "+PlayerData.notHoldJumpForce);
		Debug.Log("moveSpeed: "+PlayerData.moveSpeed);
	}
}
